package com.airlines.crewassignment.controller.crewlocation;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.airlines.base.config.response.ResponseResult;
import com.airlines.common.Page;
import com.airlines.common.R;
import com.airlines.crewassignment.entity.crewlocation.CrewTaskAssignment;
import com.airlines.crewassignment.service.crew.CrewTaskService;
import com.airlines.crewassignment.service.crewlocation.CrewTaskAssignmentService;

import mercury.framework.security.Authorization;

/**
 * @author chenjiantong
 * @date 2018/09/04 16:23:40
 * @company xxxx Airlines
 * @description 任务安排
 */
@Controller
@RequestMapping("/crewTaskAssignment")

public class CrewTaskAssignmentController {

	private static final String PREFIX = "/crewlocation";

	@Autowired
	CrewTaskAssignmentService crewTaskAssignmentService;
	@Autowired
	CrewTaskService crewTaskService;

	/**
	 * 进入页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "", method = { RequestMethod.GET })
	// @RequiresPermissions("crewlocation:crewAbsent:list")
	String toCrewAbsentPage(Model model, String moduleFlag) {
		model.addAttribute("moduleFlag", moduleFlag);
		return PREFIX + "/crewTaskAssignmentList";
	}

	/**
	 * 多条件模糊 查询并且分页显示
	 * 
	 * @param crewTaskAssignment
	 * @param offset
	 * @param limit
	 * @return
	 */
	@RequestMapping(value = "/list")
	@ResponseBody
	// @RequiresPermissions("crewlocation:crewAbsent:list")
	public Page list(CrewTaskAssignment crewTaskAssignment, String offset, String limit) {
		Page page = new Page();
		page.setPageNumber((Integer.valueOf(offset) / (Integer.valueOf(limit))));
		page.setPageSize(Integer.valueOf(limit));
		page = crewTaskAssignmentService.list(crewTaskAssignment, page);
		return (page);
	}

	/**
	 * 进入新增页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "/add", method = { RequestMethod.GET })
	// @RequiresPermissions("crewlocation:crewAbsent:add")
	String add(Model model, String moduleFlag) {
		model.addAttribute("moduleFlag", moduleFlag);
		return PREFIX + "/crewTaskAssignmentAdd";
	}

	/**
	 * 保存
	 * 
	 * @param crewTaskAssignment
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/save", method = { RequestMethod.POST })
	// @RequiresPermissions("crewlocation:crewAbsent:add")
	public Map<String, Object> save(CrewTaskAssignment crewTaskAssignment, HttpServletRequest request) {
		Map<String, Object> result = new HashMap<>();
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		try {
			Date startDate = format.parse(crewTaskAssignment.getTaskStart());
			Date endDate = format.parse(crewTaskAssignment.getTaskEnd());
			String userNo = Authorization.getUser().getUserNo();
			ResponseResult<String> rs = crewTaskService.saveCrewTaskAssignmentCk(userNo, startDate, endDate);
			if (rs.isSuccess()) {
				crewTaskAssignment.setUpdateBy(userNo);
				Date currentTime = new Date();
				SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				crewTaskAssignment.setUpdateTime(formater.format(currentTime));
				if (crewTaskAssignmentService.insert(crewTaskAssignment) > 0) {
					result.put("code", "200");
				} else {
					result.put("code", "201");
				}
			}
			if (rs.isSuccess() == false) {
				result.put("code", "201");
			}
			if (rs.getMessage() != null && rs.getMessage() != "") {
				result.put("message", rs.getMessage());
			}
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			result.put("code", "201");
		}
		return result;
	}

	/**
	 * 进入编辑页面
	 * 
	 * @param model
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/edit", method = { RequestMethod.GET })
	// @RequiresPermissions("crewlocation:crewAbsent:edit")
	String edit(Model model, Integer id, String moduleFlag) {
		CrewTaskAssignment crewTaskAssignment = crewTaskAssignmentService.get(id);
		model.addAttribute("crewTaskAssignment", crewTaskAssignment);
		model.addAttribute("moduleFlag", moduleFlag);
		return PREFIX + "/crewTaskAssignmentEdit";
	}

	/**
	 * 修改
	 * 
	 * @param crewTaskAssignment
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/update", method = { RequestMethod.POST })
	// @RequiresPermissions("crewlocation:crewAbsent:edit")
	public R update(CrewTaskAssignment crewTaskAssignment, HttpServletRequest request) {

		crewTaskAssignment.setUpdateBy(Authorization.getUser().getUserNo());
		Date currentTime = new Date();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		crewTaskAssignment.setUpdateTime(format.format(currentTime));
		if (crewTaskAssignmentService.update(crewTaskAssignment) > 0) {
			return R.ok();
		}
		return R.error();
	}

	/**
	 * 进入查看页面
	 * 
	 * @param model
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/view", method = { RequestMethod.GET })
	// @RequiresPermissions("crewlocation:crewAbsent:list")
	String view(Model model, Integer id) {
		CrewTaskAssignment crewTaskAssignment = crewTaskAssignmentService.get(id);
		model.addAttribute("crewTaskAssignment", crewTaskAssignment);
		model.addAttribute("isReadonly", 1);
		return PREFIX + "/crewTaskAssignmentEdit";
	}

	/**
	 * 删除
	 * 
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/delete", method = { RequestMethod.POST })
	// @RequiresPermissions("crewlocation:crewAbsent:delete")
	public R delete(Integer id) {
		if (crewTaskAssignmentService.delete(id) > 0) {
			return R.ok();
		}
		return R.error();
	}

	/**
	 * 检查代码是否存在
	 * 
	 * @param crewTaskAssignment
	 * @return
	 */
	@RequestMapping("check")
	@ResponseBody
	public R check(CrewTaskAssignment crewTaskAssignment) {
		List<CrewTaskAssignment> crewTaskAssignmentsList = crewTaskAssignmentService.list(crewTaskAssignment);
		return R.ok().put("valid", crewTaskAssignmentsList.size() == 0);
	}
}
